﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Basic.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace Jw.Web.Areas.Basic.Controllers
{
    public class CkzdController : Controller
    {

        //
        // GET: /Basic/Ckzd/

        public ActionResult Index()
        {
            var model = new
            {
                idField = "Ckzd_Ckbh", //主键
                urls = BasicHelper.GetIndexUrls("Ckzd"),
                resx = BasicHelper.GetIndexResx("仓库字典"),
                dataSource = new
                {
                },
                form = new
                {
                    Ckzd_Ckbh = "", //仓库编号
                    Ckzd_Ckmc = "",   //仓库名称
                    Ckzd_Ckdd = "",   //仓库地点
                    Ckzd_Ckzg = ""  //仓库主管
                },
                defaultRow = new
                {
                    //默认值
                    Ckzd_State = 1,
                    Ckzd_Address = "",
                    Ckzd_Lxdh = "",
                    Ckzd_Email = ""
                },
                setting = new
                {
                    postListFields = new string[] { "Ckzd_Ckbh", "Ckzd_Ckmc", "Ckzd_Ckdd", "Ckzd_Address", "Ckzd_Lxdh", "Ckzd_Ckzg", "Ckzd_Email", "Ckzd_State" }
                }
            };

            return View(model);

        }
    }

    public class CkzdApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Ckzd_Ckbh DESC'>
            <select>*</select>
                <from>l_Ckzd</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Ckzd_Ckbh' cp='like'></field>
                    </where>
            </settings>");
            var service = new Jw.Models.l_CkzdService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }


        public string GetNewBillNo()
        {
            var service = new l_CkzdService();
            return service.GetNewCkzdCode();
        }

        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Ckzd_Ckbh' cp='equal'></field></where>
            </settings>", typeof(l_Ckzd).Name);
            var service = new l_CkzdService();
            var result = service.Edit(null, listWrapper, data);


        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new l_CkzdService().Delete(ParamDelete.Instance().AndWhere("Ckzd_Ckbh", id));
            BasicHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Aqjb_BillNo={0}]，请重试或联系管理员！", id);
        }

    }
}
